//
//  SYDPasswordPayment.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/11.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDPasswordPayment.h"
#define kDotSize CGSizeMake (10, 10) //密码点的大小
#define kDotCount 6  //密码个数
#define K_Field_Height 44  //每一个输入框的高度等于当前view的高度
@interface SYDPasswordPayment()
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIImageView *separeImageView;
@property(nonatomic,strong)UIButton *closeBtn;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSMutableArray *dotArray; //用于存放黑色的点点
@property(nonatomic,strong)UIButton *forgetBtn;//忘记密码

@end
@implementation SYDPasswordPayment

#pragma mark - init
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = CONTENT_BG_COLOR;
        [self initPwdTextField];
    }
    return self;
}
- (void)initPwdTextField
{
    [self fadeIn];
    [self addSubview:self.separeImageView];
    //每个密码输入框的宽度
    CGFloat width = self.textField.width / kDotCount;
    
    //生成分割线
    for (int i = 0; i < kDotCount - 1; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.textField.frame) + (i + 1) * width, 55, 1, K_Field_Height)];
        lineView.backgroundColor = setPayWordColor;
        [self addSubview:lineView];
    }
    
    self.dotArray = [[NSMutableArray alloc] init];
    //生成中间的点
    for (int i = 0; i < kDotCount; i++) {
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.textField.frame) + (width - kDotCount) / 2 + i * width, CGRectGetMinY(self.textField.frame) + (K_Field_Height - kDotSize.height) / 2, kDotSize.width, kDotSize.height)];
        dotView.backgroundColor = [UIColor blackColor];
        dotView.layer.cornerRadius = kDotSize.width / 2.0f;
        dotView.clipsToBounds = YES;
        dotView.hidden = YES; //先隐藏
        [self addSubview:dotView];
        //把创建的黑色点加入到数组中
        [self.dotArray addObject:dotView];
    }
    
    [self addSubview:self.titleLabel];
    WEAKSELF
    [[self.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        if (weakSelf.closeBlock) {
            weakSelf.closeBlock();
        }
    }];
    
    [self addSubview:self.forgetBtn];
    [[self.forgetBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.forgetBlock) {
            weakSelf.forgetBlock();
        }
    }];
}
#pragma mark ---animation methods
-(void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }completion:^(BOOL finished) {
        [self.textField becomeFirstResponder];
    }];
}

#pragma mark---UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"变化%@", string);
    if([string isEqualToString:@"\n"]) {
        //按回车关闭键盘
        [textField resignFirstResponder];
        return NO;
    } else if(string.length == 0) {
        //判断是不是删除键
        return YES;
    }
    else if(textField.text.length >= kDotCount) {
        //输入的字符个数大于6，则无法继续输入，返回NO表示禁止输入
        NSLog(@"输入的字符个数大于6，忽略输入");
        return NO;
    } else {
        return YES;
    }
}
/**
 *  清除密码
 */
- (void)clearUpPassword
{
    
    self.textField.text = @"";
    [self textFieldDidChange:self.textField];
}
/**
 *  重置显示的点
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    NSLog(@"%@", textField.text);
    for (UIView *dotView in self.dotArray) {
        dotView.hidden = YES;
    }
    for (int i = 0; i < textField.text.length; i++) {
        ((UIView *)[self.dotArray objectAtIndex:i]).hidden = NO;
    }
    if (textField.text.length == kDotCount) {
        
        //回调
        [self.delegate passwordTextFieldChanged:textField.text];
    }
}
#pragma mark---Lazy
- (UITextField *)textField
{
    if (!_textField) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 55, self.width - 30, K_Field_Height)];
        _textField.backgroundColor = [UIColor whiteColor];
        //输入的文字颜色为白色
        _textField.textColor = [UIColor whiteColor];
        //输入框光标的颜色为白色
        _textField.tintColor = [UIColor whiteColor];
        _textField.delegate = self;
        _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.layer.borderColor = setPayWordColor.CGColor;
        _textField.layer.borderWidth = 1;
        [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];
    }
    return _textField;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.width / 2 - 52.5, 15, 115, 15)];
        _titleLabel.textColor = TITLE_COLOR;
        _titleLabel.text = @"请输入支付密码";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
    return _titleLabel;
}
-(UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 15 - 20, 15, 20, 20)];
        [_closeBtn setImage:[UIImage imageNamed:@"icon_purse_close"] forState:UIControlStateNormal];
        [self addSubview:_closeBtn];
    }
    return _closeBtn;
}
-(UIImageView *)separeImageView{
    if (!_separeImageView) {
        _separeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 44, self.width - 30, .5)];
        _separeImageView.image = [UIImage imageNamed:@"separator"];
    }
    return _separeImageView;
}
-(UIButton *)forgetBtn{
    if (!_forgetBtn) {
        _forgetBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 75, 109, 60, 15)];
        [_forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetBtn setTitleColor:SUCCESS_COLOR forState:UIControlStateNormal];
        _forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _forgetBtn;
}
@end
